import { defineConfig, loadEnv } from 'vite'
import createVitePlugins from './vite/plugins'
import * as path from 'path'
const resolve = (p: string) => {
  return path.resolve(__dirname, p)
}

export default ({ mode }) => {
  const envConfig = loadEnv(mode, process.cwd())
  return defineConfig({
    plugins: createVitePlugins(),
    resolve: {
      alias: {
        '@': resolve('./src'),
      },
    },
    server: {
      host: envConfig.VITE_host,
      port: Number(envConfig.VITE_port) || 3000,
      proxy: {
        '/api': {
          target: 'http://localhost:9999',
          ws: false,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },
    build: {
      outDir: envConfig.VITE_fileName || 'dist',
    },
  })
}
